package cn.com.darly.videoandroidpm.ui.conversation.video;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import com.darly.common.DLog;
import com.darly.common.ToastApp;
import com.darly.dview.framework.ContentBinder;
import com.darly.dview.framework.ViewsBinder;
import com.darly.dview.widget.camera.util.ImageLoaderUtil;

import java.util.LinkedHashMap;
import java.util.Map;

import cn.com.darly.videoandroidpm.R;
import cn.com.darly.videoandroidpm.base.BaseActivity;
import cn.com.darly.videoandroidpm.bean.LoginInfo;
import cn.com.darly.videoandroidpm.common.SharePreferHelp;
import cn.com.darly.videoandroidpm.common.StaticListener;
import cn.com.darly.videoandroidpm.common.StaticListener.OnCallConsoleListener;
import cn.com.darly.videoandroidpm.ui.biz.VideoAcceptListener;
import cn.com.darly.videoandroidpm.ui.presenter.VideoAcceptPresenter;
import cn.com.darly.videoandroidpm.widget.callbutton.CallToggleButton;
import cn.com.darly.videoandroidpm.widget.callbutton.CallToggleButton.OnSliderTriggeredListener;
import cn.com.darly.videoandroidpm.widget.mover.Techniques;
import cn.com.darly.videoandroidpm.widget.mover.YoYo;

/** 用户接受视频呼叫的页面。（用户来电展示页面）
 * @author Darly/张宇辉/2018/5/29 9:18
 * @version 1.0/cn.com.darly.videoandroidpm.ui.conversation.video
 */
@ContentBinder(R.layout.activity_video_accept)
public class VideoAcceptActivity extends BaseActivity implements VideoAcceptListener.VideoAcceptInterface,OnSliderTriggeredListener,OnCallConsoleListener{

    @ViewsBinder(R.id.id_video_accept_msg)
    TextView id_video_accept_msg;

    @ViewsBinder(R.id.id_video_accept_icon)
    ImageView id_video_accept_icon;
    @ViewsBinder(R.id.id_video_accept_name)
    TextView id_video_accept_name;

    @ViewsBinder(R.id.id_video_accept_calltoggle)
    CallToggleButton id_video_accept_calltoggle;

    @ViewsBinder(R.id.id_video_start_wiving_left)
    ImageView id_video_start_wiving_left;
    @ViewsBinder(R.id.id_video_start_wiving_right)
    ImageView id_video_start_wiving_right;

    private YoYo.YoYoString left;
    private YoYo.YoYoString right;
    private VideoAcceptPresenter presenter;

    private LoginInfo friend;

    private LoginInfo user;

    private String rtmp;

    @Override
    protected void initView(Bundle savedInstanceState) {
        presenter = new VideoAcceptPresenter(this);
        friend = (LoginInfo) getIntent().getSerializableExtra("LoginInfo");
        if (friend==null) {
            ToastApp.showToast("好友信息为空");
            finish();
        }
        user = (LoginInfo) SharePreferHelp.getValue("LoginInfo");
        if (user == null){
            ToastApp.showToast("用户信息错误");
            finish();
        }
        rtmp = getIntent().getStringExtra("RTMP");
        if (TextUtils.isEmpty(rtmp)){
            ToastApp.showToast("流媒体地址丢失");
            return;
        }
    }

    @Override
    protected void loadData() {
        ImageLoaderUtil.getInstance().loadImageNor(friend.getIcon(),id_video_accept_icon);
        id_video_accept_name.setText(friend.getName());

        //使图片渐变
        if (left != null) {
            left.stop(true);
        }
        left = YoYo.with(Techniques.OutRight)
                .duration(2000)
                .repeat(YoYo.INFINITE)
                .pivot(YoYo.CENTER_PIVOT, YoYo.CENTER_PIVOT)
                .interpolate(new AccelerateDecelerateInterpolator())
                .playOn(id_video_start_wiving_left);
        if (right != null) {
            right.stop(true);
        }
        right = YoYo.with(Techniques.OutLeft)
                .duration(2000)
                .repeat(YoYo.INFINITE)
                .pivot(YoYo.CENTER_PIVOT, YoYo.CENTER_PIVOT)
                .interpolate(new AccelerateDecelerateInterpolator())
                .playOn(id_video_start_wiving_right);
    }

    @Override
    protected void initListener() {
        id_video_accept_calltoggle.setOnSliderTriggeredListener(this);
        StaticListener.setOnCallConsoleListener(this);
    }

    @Override
    public void onVideoAcceptSuccess(String  info) {
        id_video_accept_msg.setText(user.getName()+"接通"+friend.getName()+"的视频请求");
        //成功连接之后，跳转页面
        Intent intent = new Intent(this,VideoConnectionActivity.class);
        intent.putExtra("LoginInfo",friend);
        intent.putExtra("OWN",info);//这个是自己的流媒体地址
        intent.putExtra("FWN",rtmp);//这个是好友的流媒体地址
        startActivity(intent);
        DLog.i(getClass().getSimpleName()+"onVideoAcceptSuccess 执行了一次");
        end();
    }

    @Override
    public void onVideoAcceptFailed(String info) {

    }

    @Override
    public void end() {
        finish();
    }

    @Override
    public void setDisableClick() {

    }

    @Override
    public void setEnableClick() {

    }

    @Override
    public void RightEvent() {
        //掛掉電話
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("friendid", friend.getId());
        presenter.onRefuse(params);
        finish();
    }

    @Override
    public void LeftEvent() {
        //接聽電話
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("friendid", friend.getId());
        presenter.onAccept(params);
    }

    @Override
    public void callConsole(String key) {
        //用戶直接關閉視頻請求。
        finish();
    }
}
